package com.books4sale.admin.service;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.books4sale.admin.dto.Profile;
import com.books4sale.admin.dto.tools.Helper;
import com.books4sale.admin.dto.tools.PMF;

public class WelcomeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String redirectURL = "/admin/index.jsp";
		if (Helper.get().isUserProfileExists() == null) {
			Helper.get().createNewProfile();
//			resp.sendRedirect(redirectURL + "?user");
			resp.getWriter().write(
					"Welcome " + Helper.get().getCurrentUser().getEmail()
							+ ", new profile has created for you!");

		} else {
//			resp.sendRedirect(redirectURL + "?books");
			resp.getWriter().write(
					"Welcome " + Helper.get().getCurrentUser().getEmail()
							+ ", You are already in our database");
		}
	}

//	// is user exists
//	private boolean isUserExists() {
//		PersistenceManager pm = PMF.get().getPersistenceManager();
//		Query query = pm.newQuery(Profile.class);
//		query.setFilter("shopEmail == currentUserEmail");
//		query.declareParameters("String currentUserEmail");
//
//		List<Profile> result = (List<Profile>) query.execute(Helper.get()
//				.getCurrentUser().getEmail());
//		System.out.println(result.size());
//		return result.size() == 0 ? false : true;
//	}
}
